// components/findlost/findlost.js
const app = getApp()
const db = wx.cloud.database()
var sev = require('../../utils/sev.js');
Component({
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    fileList:[],
    cloudlist:[],
    title: '',
  },

  /**
   * 组件的方法列表
   */
  methods: {
    getTitle(e) {
      this.setData({
        title: e.detail.value
      });
    },
    syncfiles(){
      var _ = this;
      wx.showLoading();
      //console.log(_.data.fileList)
      _.uploadFile(0,_.data.fileList)
    },
    uploadFile(index, tempFilePaths) {
      var _ = this;
      console.log(tempFilePaths)
      const fname = tempFilePaths[index].name;
      wx.cloud.uploadFile({
        cloudPath: 'file/' + '(' + sev.getformatTime(new Date())+')'+fname,
          filePath: tempFilePaths[index].path,
          success:f=>{
            //console.log(f);
            var str = f.fileID;
            var filetype = str.substring(str.lastIndexOf('.') + 1);
            this.setData({
              cloudlist:this.data.cloudlist.concat({
                filename:fname,
                filetype,
                fileurl:str
              })
            })
            if(index < tempFilePaths.length - 1){
              this.uploadFile(index+1,tempFilePaths);
            }else{
              this.toUpdate();
              //wx.hideLoading();
            }
          }
      })
    },
    
    toUpdate() {
      //信息上传
      var _ = this;
      console.log(_.data.cloudlist)
      wx.cloud.callFunction({
        name:'kindb',
        data:{
          opr:'add',
          tablename:'NEXUSET',
          data:{
            sofname:_.data.title,
            source:'cloud',
            status:'private',
            type:'set',
            uid:app.globalData.openid,
            uschool:app.sel_item.name,
            dbdate:sev.getformatTime(new Date()),
            fileurls:_.data.cloudlist
          }
        },
        success:f=>{
          wx.hideLoading();//
          wx.showModal({
            title:'审核',
            content:'内容审核通过后可展示',
            showCancel:false,
            success:r=>{
              if(r.confirm){
                wx.navigateBack();
              }
            }
          })  
        }
      })
    },
    chooseFiles(){
      var that = this;
      wx.chooseMessageFile({
        success(res){
          var str  = res.tempFiles[0].path;

          //console.log(str.substring(str.lastIndexOf('.')+1))
          console.log(res)
          that.setData({
            fileList:that.data.fileList.concat(res.tempFiles)
          })
        }
      })
    }
  }
})